package com.ycg.store.controller;

import com.ycg.store.common.Result;
import com.ycg.store.pojo.Warehouse;
import com.ycg.store.service.WarehouseService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author YCG
 * @date 2024-05-23 11:23
 * @description 仓库控制器
 */
@RestController
@RequestMapping("/warehouse")
public class WarehouseController {

    @Resource
    private WarehouseService warehouseService;

    /**
     * 根据租户id获取仓库
     *
     * @param id 租户id
     * @return List<Warehouse>
     */
    @GetMapping("/{id}")
    public Result<List<Warehouse>> getWarehouse(@PathVariable String id) {
        return warehouseService.getByTenantId(id);
    }

    /**
     * 新增仓库
     *
     * @param warehouse 新增仓库信息
     * @return ok
     */
    @PostMapping
    public Result<String> postWarehouse(@RequestBody @Validated Warehouse warehouse) {
        return warehouseService.save(warehouse)
                ? Result.ok("新增成功") : Result.error("新增失败");
    }


    /**
     * 根据租户id修改仓库
     *
     * @param warehouse 更新后的仓库信息
     * @return ok
     */
    @PutMapping
    public Result<String> putWarehouse(@RequestBody @Validated Warehouse warehouse) {
        return warehouseService.updateById(warehouse)
                ? Result.ok("更新成功") : Result.error("更新失败");
    }

    /**
     * 根据租户id修改仓库
     *
     * @param id 仓库id
     * @return ok
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteWarehouse(@PathVariable String id) {
        return warehouseService.removeById(id)
                ? Result.ok("删除成功") : Result.error("删除失败");
    }


}
